﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using DocumentManagement.Models;
using DocumentManagement.Views;
using System.IO;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace DocumentManagement.Controllers
{
    [HandleError]
    public class BaoGaoController : BaseController
    {
        [Authentication(PermissionLevel.BaoGaoOperationList)]
        public ActionResult Index()
        {
            return View();
        }

        [Authentication(PermissionLevel.BaoGaoCreate)]
        public ActionResult ShowCreateBaoGao(Guid? id)
        {
            YuGuDan ygd = null;
            if (id != null)
            {
                ygd = this.Model.GetYuGuDan(id.Value);
            }
            else
            {
                ygd = new YuGuDan();
                ygd.gjshd = DateTime.Now;
                ygd.jgrq = DateTime.Now.Year;
            }

            return View("CreateBaoGao", new CreateBaoGaoViewData() { YGD = ygd });
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [Authentication(PermissionLevel.BaoGaoCreate)]
        public ActionResult CreateBaoGao(BaoGao baogao, Guid? ygId, HttpPostedFileBase file)
        {
            BaoGao bg = this.Model.CreateBaoGao(this.AuthenticatedUser.id, baogao, ygId, file == null ? null : file.InputStream);

            return View("Index");
        }

        [Authentication(PermissionLevel.BaoGaoUpdate)]
        public ActionResult ShowUpdateAuditFailedBaoGao(Guid id)
        {
            var bg = this.Model.GetBaoGao(id);

            return View("UpdateAuditFailedBaoGao", new UpdateAuditFailedBaoGaoViewData() { BG = bg });
        }

        [Authentication(PermissionLevel.BaoGaoUpdate)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateBaoGao(BaoGao baogao, HttpPostedFileBase file)
        {
            BaoGao bg = this.Model.UpdateBaoGao(baogao, file == null ? null : file.InputStream);

            return RedirectToAction("ShowAuditFailedBaoGao");
        }


        [Authentication(PermissionLevel.BaoGaoViewDetail)]
        public ActionResult ShowBaoGaoDetail(Guid id)
        {
            BaoGao bg;
            BaoGaoShenPiLiShi[] history = this.Model.GetBaoGaoDetail(id, out bg);
            return View("BaoGaoDetail",
                new BaoGaoDetailViewData() { BG = bg, BGAuditHistory = history });
        }

        [Authentication(PermissionLevel.BaoGaoUpdate)]
        public ActionResult ShowAuditFailedBaoGao()
        {
            var bgs = this.Model.GetAuditFailedBaoGao(this.AuthenticatedUser.id);
            return View("AuditFailedBaoGao", new AuditFailedBaoGaoViewData() { BGs = bgs }); ;
        }

        [Authentication(PermissionLevel.BaoGaoAudit1)]
        public ActionResult ShowAudit1BaoGao()
        {
            var bgs = this.Model.GetAudit1BaoGao();
            return View("AuditBaoGao", new AuditBaoGaoViewData() { BGs = bgs });
        }

        [Authentication(PermissionLevel.BaoGaoAudit2)]
        public ActionResult ShowAudit2BaoGao()
        {
            var bgs = this.Model.GetAudit2BaoGao();
            return View("AuditBaoGao", new AuditBaoGaoViewData() { BGs = bgs });
        }

        [Authentication(PermissionLevel.BaoGaoAudit3)]
        public ActionResult ShowAudit3BaoGao()
        {
            var bgs = this.Model.GetAudit3BaoGao();
            return View("AuditBaoGao", new AuditBaoGaoViewData() { BGs = bgs });
        }

        [Authentication(PermissionLevel.BaoGaoAudit1)]
        public ActionResult Audit1BaoGaoSuccess(Guid id, string comment)
        {
            this.Model.Audit1BaoGaoSuccess(id, comment, this.AuthenticatedUser.id);
            return RedirectToAction("ShowAudit1BaoGao");
        }

        [Authentication(PermissionLevel.BaoGaoAudit2)]
        public ActionResult Audit2BaoGaoSuccess(Guid id, string comment)
        {
            this.Model.Audit2BaoGaoSuccess(id, comment, this.AuthenticatedUser.id);
            return RedirectToAction("ShowAudit2BaoGao");
        }

        [Authentication(PermissionLevel.BaoGaoAudit3)]
        public ActionResult Audit3BaoGaoSuccess(Guid id, string comment)
        {
            this.Model.Audit3BaoGaoSuccess(id, comment, this.AuthenticatedUser.id);
            return RedirectToAction("ShowAudit3BaoGao");
        }

        [Authentication(PermissionLevel.BaoGaoAudit1| PermissionLevel.BaoGaoAudit2 | PermissionLevel.BaoGaoAudit3, false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Audit(Guid id, string comment, bool isSuccess)
        {
            if (isSuccess)
            {
                return AuditSuccess(id, comment);
            }
            else
            {
                return AuditFail(id, comment);
            }
        }

        [Authentication(PermissionLevel.BaoGaoAudit1 | PermissionLevel.BaoGaoAudit2 | PermissionLevel.BaoGaoAudit3, false)]
        private ActionResult AuditSuccess(Guid id, string comment)
        {
            var bg = this.Model.GetBaoGao(id);
            switch ((BaoGaoState)bg.state)
            {
                case BaoGaoState.Audit1:
                    return RedirectToAction("Audit1BaoGaoSuccess", new { id = id, comment = comment });
                case BaoGaoState.Audit2:
                    return RedirectToAction("Audit2BaoGaoSuccess", new { id = id, comment = comment });
                case BaoGaoState.Audit3:
                    return RedirectToAction("Audit3BaoGaoSuccess", new { id = id, comment = comment });
                default:
                    throw new InvalidOperationException("不支持的操作");
            }
        }

        [Authentication(PermissionLevel.BaoGaoAudit1 | PermissionLevel.BaoGaoAudit2 | PermissionLevel.BaoGaoAudit3, false)]
        private ActionResult AuditFail(Guid id, string comment)
        {
            this.Model.AuditFail(id, comment, this.AuthenticatedUser.id);
            return View("Index");
        }

        [Authentication(PermissionLevel.BaoGaoAttachmentDownload)]
        public ActionResult DownloadBaoGaoAttachment(Guid id)
        {
            var bg = this.Model.GetBaoGao(id);
            if (bg.hasAttachment)
            {
                return this.File(YaoHuaModel.GetFilePath(id), "application/zip", id+".zip");
            }

            throw new InvalidOperationException("不支持的操作--没有报告");
        }

        [Authentication(PermissionLevel.BaoGaoSearch)]
        public ActionResult ShowSearch()
        {
            return View("Search");
        }

        [Authentication(PermissionLevel.BaoGaoSearch)]
        public ActionResult Search(string wjbh, string wtly, string wtf, string xmzl, DateTime? gjshd, DateTime? pgwchrq, string gjzyry, string wtr, string search, string summary)
        {
            if (!string.IsNullOrEmpty(summary))
            {
                return DownloadSummary(wjbh, wtly, wtf, xmzl, gjshd, pgwchrq, gjzyry, wtr);
            }
            else
            {
                var bgs = this.Model.SearchBaoGao(wjbh, wtly, wtf, xmzl, gjshd, pgwchrq, gjzyry, wtr);
                return View("SearchResult", new DocumentManagement.Views.BG.SearchResultViewData() { BGs = bgs });
            }
        }

        [Authentication(PermissionLevel.BaoGaoAfterAuditProcess)]
        public ActionResult ShowBaoGaoNeedToCharge()
        {
            var bgs = this.Model.GetBaoGaoNeedToCharge();
            var es = this.Model.GetAllUsers();

            return View("NeedToCharge", new NeedToChargeViewData() { BGs = bgs, Employees = es });
        }

        [Authentication(PermissionLevel.BaoGaoAfterAuditProcess)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AssginBaoGaoToChargeMoney(Guid id, int assigneeId, double kpje, string fpbh)
        {
            this.Model.AssginBaoGaoToChargeMoney(id, assigneeId, kpje, fpbh);

            return RedirectToAction("ShowBaoGaoNeedToCharge");
        }

        [Authentication(PermissionLevel.BaoGaoSearch)]
        public ActionResult DownloadSummary(string wjbh, string wtly, string wtf, string xmzl, DateTime? gjshd, DateTime? pgwchrq, string gjzyry, string wtr)
        {
            string file = this.Model.GenerateBaoGaoSumary(wjbh, wtly, wtf, xmzl, gjshd, pgwchrq, gjzyry, wtr);
            return this.File(file, "application/msexcel", "Summary.xls");
        }

        [Authentication(PermissionLevel.Special)]
        public ActionResult LoadLegacyDoc()
        {
            string dir = this.Server.MapPath("~/legacydoc");

            foreach (var f in Directory.GetFiles(dir))
            {   
                try
                {
                    legacydoc doc;

                    using (FileStream fs = new FileStream(f, FileMode.Open, FileAccess.Read))
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(legacydoc));
                        doc = (legacydoc)xs.Deserialize(fs);
                    }

                    var baogao = new BaoGao();

                    baogao.wjbh = doc.bh;
                    baogao.gjqshrq = doc.rq;
                    baogao.xmqy = doc.qy;
                    baogao.xmzl = doc.zld;
                    baogao.wtf = doc.wtr;
                    baogao.jzhmj = (double)doc.mj;
                    baogao.wtr = "NA";
                    baogao.wtly = doc.ly;
                    baogao.jgrq = doc.jgrq.Year;
                    baogao.pgzj = (double)doc.pgzj;
                    baogao.pgdj = (double)doc.pgdj;
                    baogao.pgwchrq = doc.bgwchrq;
                    baogao.gjzyry = doc.pgr;

                    baogao.tdmj = 0;
                    baogao.yt = 0;
                    baogao.jzhcg = doc.zlc;
                    baogao.pgjzh = 0;
                    baogao.xchkchy = "";
                    baogao.gjshd = doc.gjshd;
                    baogao.gjzyry = doc.pgr;

                    BaoGao bg = this.Model.CreateBaoGao(this.AuthenticatedUser.id, baogao, null, null);
                }
                catch
                {
                }
            }

            return View("index");
        }
    }
}
